package config

import (
	"encoding/json"
	"log"
)

type Config_Service struct {
	ServiceName            string
	Address                string
	ServiceRegistry        bool
	ServiceTTL             int64
	UseTLS                 bool
	TrafficInboundRestrict string
}

type Config_Client struct {
	ServiceName      string
	Address          string
	ServiceDiscovery bool
	UseTLS           bool
	UsePool          bool
}

var ServiceMap = make(map[string]Config_Service)
var ClientMap = make(map[string]Config_Client)

func (i *Initializer) MicroService() {
	//开始解析service配置
	services := cfg.GetStringSlice("MicroService.Service")
	for _, v := range services {
		var service Config_Service
		if err := json.Unmarshal([]byte(v), &service); err == nil {
			ServiceMap[service.ServiceName] = service
		} else {
			log.Fatalln("配置文件中[", v, "]无法转换为对象:", err)
		}
	}

	//开始解析client配置
	clients := cfg.GetStringSlice("MicroService.Client")
	for _, v := range clients {
		var client Config_Client
		if err := json.Unmarshal([]byte(v), &client); err == nil {
			ClientMap[client.ServiceName] = client
		} else {
			log.Fatalln("配置文件中[", v, "]无法转换为对象:", err)
		}
	}
}
